﻿using System;
using Noea.TogSim.Model.Interfaces;
using Noea.TogSim.Model.Signal.States;

namespace Noea.TogSim.Model.Signal
{
    public class SimpleSignal : Signal
    {
        ISignalState _signalState;
        ISignal _associatedSignal;

        static ISignalState _goState = new LightSignalState(0, "Go", "green");
        static ISignalState _stopState = new LightSignalState(1, "Stop", "red");
        static ISignalState _waitState = new LightSignalState(2, "Wait", "yellow", true);

        public SimpleSignal(int Id, ISignalState state)
            : base(Id)
        {
            _signalState = state;
        }

        public SimpleSignal(int Id, ISignalState state, ISignal asignal)
            : base(Id)
        {
            _signalState = state;
            _associatedSignal = asignal;
        }
        public override ISignalState State
        {
            get
            {
                return _signalState;
            }
            set
            {
                //Console.WriteLine("Signal sættes til " + value);
                ISignalState newValue = value;
                ISignalState oldValue = _signalState;
                if (oldValue != newValue)
                {
                    _signalState = newValue;
                    SignalHandler tempEvent = OnChange;

                    //Console.WriteLine(OnChange.GetInvocationList());
                    if (tempEvent != null)
                    {
                        SignalEventArgs args = new SignalEventArgs(this, oldValue, newValue);
                        tempEvent(this, args);
                    }
                }
            }
        }

        public override ISignal AssociatedSignal { get { return _associatedSignal; } set { _associatedSignal = value; } }
        static public ISignalState Go { get { return _goState; } }
        static public ISignalState Stop { get { return _stopState; } }
        static public ISignalState Wait { get { return _waitState; } }

        public void SetGo()
        {
            State = _goState;
            //Console.WriteLine("Signal " + Id + ": GO");
        }

        public void SetStop()
        {
            State = _stopState;
            //Console.WriteLine("Signal " + Id + ": STOP");
        }

        public void SetWait()
        {
            State = _waitState;
            //Console.WriteLine("Signal " + Id + ": Wait");
        }
        public override event SignalHandler OnChange;
    }
}
